package com.lzq.course.server.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lzq.course.server.domain.Section;
import com.lzq.course.server.domain.SectionExample;
import com.lzq.course.server.dto.SectionDto;
import com.lzq.course.server.dto.PageDto;
import com.lzq.course.server.dto.SectionPageDto;
import com.lzq.course.server.enums.SectionChargeEnum;
import com.lzq.course.server.mapper.SectionMapper;
import com.lzq.course.server.util.CopyUtil;
import com.lzq.course.server.util.UuidUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.Date;

@Service
public class SectionService {

    @Resource
    private SectionMapper sectionMapper;

    @Resource
    private CourseService courseService;

    /**
     * example 这个文件很有用
     * 可以帮我们生成where order 查询
     * 不用单独写sql的增删改查
     */
    public void list(SectionPageDto sectionPageDto) {
        // 分页查询 页码不是从0开始
        PageHelper.startPage(sectionPageDto.getPage(), sectionPageDto.getSize());

        // 排序
        SectionExample sectionExample = new SectionExample();
        sectionExample.setOrderByClause("sort asc");

        SectionExample.Criteria sectionExampleCriteria = sectionExample.createCriteria();
        if (!StringUtils.isEmpty(sectionPageDto.getCourseId())) {
            sectionExampleCriteria.andCourseIdEqualTo(sectionPageDto.getCourseId());
        }
        if (!StringUtils.isEmpty(sectionPageDto.getChapterId())) {
            sectionExampleCriteria.andChapterIdEqualTo(sectionPageDto.getChapterId());
        }

        // 查询
        List<Section> sectionList = sectionMapper.selectByExample(sectionExample);

        PageInfo<Section> pageInfo = new PageInfo<>(sectionList);
        sectionPageDto.setTotal(pageInfo.getTotal());

        List<SectionDto> sectionDtoList = CopyUtil.copyList(sectionList, SectionDto.class);

        sectionPageDto.setList(sectionDtoList);
    }

    /**
     * 保存 修改
     * 多张表的操作：要加事务。
     * @param sectionDto
     */
    @Transactional
    public void save(SectionDto sectionDto) {
        //BeanUtils.copyProperties(sectionDto, section);
        Section copySection = CopyUtil.copy(sectionDto, Section.class);

        if (StringUtils.isEmpty(sectionDto.getId())) {
            // 新增
            // 八位的 id
            copySection.setId(UuidUtil.getShortUuid());
            // 默认收费
            copySection.setCharge(SectionChargeEnum.CHARGE.getCode());
            Date now = new Date();

            sectionMapper.insert(copySection);
        } else {
            // 编辑
            sectionMapper.updateByPrimaryKey(copySection);
        }

        // 更新课程时间
        courseService.updateTime(sectionDto.getCourseId());
    }

    /**
     * 删除
     * @param id
     */
    public void delete(String id) {
        sectionMapper.deleteByPrimaryKey(id);
    }

    /**
     * 查询某一课程下的所有节
     */
    public List<SectionDto> listByCourse(String courseId) {
        SectionExample example = new SectionExample();
        example.createCriteria().andCourseIdEqualTo(courseId);
        List<Section> sectionList = sectionMapper.selectByExample(example);
        List<SectionDto> sectionDtoList = CopyUtil.copyList(sectionList, SectionDto.class);
        return sectionDtoList;
    }
}
